module Display {
	export class BasicSprite extends egret.Sprite{
		private _txInfo:egret.TextField;
		private _heros:Array<egret.Bitmap>=[];

		/**
		 * 构造函数
		 * 监听添加到舞台事件
		 */
		public constructor() {
			super();
			this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddedToStage,this);
		}

		/**
		 * 添加到舞台监听器
		 * 加载图片 
		 * @param evt:Event
		 */
		private onAddedToStage(evt:egret.Event):void{
			var imgLoader:egret.ImageLoader=new egret.ImageLoader;
			imgLoader.once(egret.Event.COMPLETE,this.imgLoaderHandler,this);
			imgLoader.load("resource/hero.png");
		}

		/**
		 * 图片加载监听器
		 * @param evt:Event
		 */
		private imgLoaderHandler(evt:egret.Event):void{
			//绘制颜色区域
			var sw:number=this.stage.stageWidth;
			var sh:number=this.stage.stageHeight;
			this.graphics.beginFill(0xf7acbc);
			this.graphics.drawRect(0,0,sw/2,sh/2);
			this.graphics.endFill();
			this.graphics.beginFill(0xdeab8a);
			this.graphics.drawRect(sw/2,0,sw/2,sh/2);
			this.graphics.endFill();
			this.graphics.beginFill(0xef5b9c);
			this.graphics.drawRect(sw/2,sh/2,sw/2,sh/2);
			this.graphics.endFill();
			this.graphics.beginFill(0xfedcbd);
			this.graphics.drawRect(0, sh / 2, sw / 2, sh / 2);
			this.graphics.endFill();
			/// 提示信息
			this._txInfo = new egret.TextField;
			this._txInfo.size = 28;
			this._txInfo.textAlign = egret.HorizontalAlign.CENTER;
			this._txInfo.textColor = 0x843900;
			this._txInfo.lineSpacing = 6;
			this._txInfo.multiline = true;
			this._txInfo.text = "点击不同色块";
			this._txInfo.x = this.stage.stageWidth / 2 - this._txInfo.width / 2;
			this._txInfo.y = 10;
			this.addChild(this._txInfo);
			//初始化四个英雄
			var hero:egret.Bitmap=new egret.Bitmap(evt.currentTarget.data);
			hero.x=sw/4-hero.width/2;
			hero.y=sh/4-hero.height/2;
			this._heros.push(hero);
			hero=new egret.Bitmap(evt.currentTarget.data);
			hero.x=3*sw/4-hero.width/2;
			hero.y=sh/4-hero.height/2;
			this._heros.push(hero);
			hero=new egret.Bitmap(evt.currentTarget.data);
			hero.x=sw/4-hero.width/2;
			hero.y=3*sh/4-hero.height/2;
			this._heros.push(hero);
			hero=new egret.Bitmap(evt.currentTarget.data);
			hero.x=3*sw/4-hero.width/2;
			hero.y=3*sh/4-hero.height/2;
			this._heros.push(hero);
			//为舞台注册触摸事件
			this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN,(evt:egret.TouchEvent)=>{
				var x:number=evt.stageX < sw/2 ? 0 : 1 ;
				var y:number=(evt.stageY < sh/2 ? 0 : 1)<<1 ;
				var r:number=x|y;
				if(this.contains(this._heros[r])){
					this.removeChild(this._heros[r]);
				}else{
					this.addChild(this._heros[r]);
				}
			},this);
		}
	}
}